# @coding utf-8
# @author errorcpp@qq.com
# @depens: 
#   init_default_env.cmake
# 函数列表：
# 1.xset_target_link_libs
# 2.xset_target_output_dir
# 3.xset_target_macros
# 4.xecho_target_macros
# 5.xecho_target_link_libs 
# 6.xset_target_include_dirs
# 7.xget_target_include_dirs  
# 8.xset_target_debug_postfix 
# 9.xset_target_postfix
# 10.xset_target_compile_options
##################
message("[cmake_util] import target_util.cmake")


###
# @brief 给目标添加链接库
#   xset_target_link_libs(xx_bin "common" ${common_libs})
#   xset_target_link_libs(xx_bin "windows" ${win_libs})
# @params TARGET_NAME
# @params PLATFORM[字符串]
# @params ...[变参] 可以是一个列表变量，也可以直接传入多个lib
function(xset_target_link_libs TARGET_NAME PLATFORM)#LIBS
    # MESSAGE(STATUS "ddddddddddddddddddddddd:(ARGC):" ${ARGC})  # 全部参数个数
    # MESSAGE(STATUS "ddddddddddddddddddddddd(ARGV):" ${ARGV})   # 全部参数列表
    # MESSAGE(STATUS "ddddddddddddddddddddddd(ARGV0):" ${ARGV0}) # 第0个参数
    # MESSAGE(STATUS "ddddddddddddddddddddddd:(ARGN):" ${ARGN})  # 传入参数 - 原型声明的参数
        
    SET(LIBS ${ARGN})
    # foreach(ARG ${ARGN})  # 遍历采集剩余参数到 LIBS
    #     MESSAGE(STATUS "ddddddddddddddddddddddd(${ARG}):" ${ARG})
    #     list(APPEND LIBS ${ARG})
    # endforeach()
    #MESSAGE(STATUS "ddddddddddddddddddddddd1:" ${LIBS})

    if(PLATFORM STREQUAL "all" OR PLATFORM STREQUAL "All" OR PLATFORM STREQUAL "common")  # 全平台生效的lib
        target_link_libraries(${TARGET_NAME} ${LIBS})
    elseif(PLATFORM STREQUAL "linux" OR PLATFORM STREQUAL "Linux")
        IF(${XCMAKE_TARGET_OS_LINUX})
            target_link_libraries(${TARGET_NAME} ${LIBS})
        ENDIF()
    elseif(PLATFORM STREQUAL "windows" OR PLATFORM STREQUAL "Windows")
        IF(${XCMAKE_TARGET_OS_WIN})
            target_link_libraries(${TARGET_NAME} ${LIBS})
        ENDIF()
    elseif(PLATFORM STREQUAL "mac" OR PLATFORM STREQUAL "Mac")
        IF(${XCMAKE_TARGET_OS_MAC})
            target_link_libraries(${TARGET_NAME} ${LIBS})
        ENDIF()
    endif()
endfunction()

# @brief 设置指定target的输出目录
# @params TARGET_NAME
# @params BIN_DIR 
# @params LIB_DIR 
function(xset_target_output_dir TARGET_NAME BIN_DIR LIB_DIR)
    set_target_properties(${TARGET_NAME} PROPERTIES
        RUNTIME_OUTPUT_DIRECTORY "${BIN_DIR}"  # exe
        LIBRARY_OUTPUT_DIRECTORY "${BIN_DIR}"  # bin_lib: *.dll *.so *.dylib
        ARCHIVE_OUTPUT_DIRECTORY "${LIB_DIR}"  # lib: *.lib *.a
        RUNTIME_OUTPUT_DIRECTORY_DEBUG "${BIN_DIR}"
        LIBRARY_OUTPUT_DIRECTORY_DEBUG "${BIN_DIR}"
        ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${LIB_DIR}"
        RUNTIME_OUTPUT_DIRECTORY_RELEASE "${BIN_DIR}"
        LIBRARY_OUTPUT_DIRECTORY_RELEASE "${BIN_DIR}"
        ARCHIVE_OUTPUT_DIRECTORY_RELEASE "${LIB_DIR}"
        RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO "${BIN_DIR}"
        LIBRARY_OUTPUT_DIRECTORY_RELWITHDEBINFO "${BIN_LIB}"
        ARCHIVE_OUTPUT_DIRECTORY_RELWITHDEBINFO "${LIB_DIR}"
    )
endfunction()

# 添加指定target的宏
function(xset_target_macros TARGET_NAME)
    SET(MACROS ${ARGN})
    target_compile_definitions(${TARGET_NAME} PRIVATE
        ${MACROS}
    )
endfunction()

# 移除指定target的宏
function(xremove_target_macro TARGET_NAME DST_MACRO)
    get_target_property(existing_macros ${TARGET_NAME} COMPILE_DEFINITIONS)
    list(REMOVE_ITEM existing_macros ${DST_MACRO})
    set_target_properties(${TARGET_NAME} PROPERTIES COMPILE_DEFINITIONS "${existing_macros}")
endfunction()

# 输出指定TARGET的宏
function(xecho_target_macros TARGET_NAME)
    get_target_property(all_macros ${TARGET_NAME} COMPILE_DEFINITIONS)
    set(echo_str)
    foreach(macro_name ${all_macros})
        if(echo_str)
            set(echo_str ${echo_str},${macro_name})
        else()
            set(echo_str ${macro_name})
        endif()
    endforeach()
    message("echo target macros: " "tareget=" ${TARGET_NAME} ",macros=" ${all_macros})
endfunction()

# 设置指定target输出post_fix
function(xset_target_postfix TARGET_NAME POSTFIX)
    set_target_properties(${TARGET_NAME} PROPERTIES SUFFIX "${POSTFIX}")
endfunction()

# 设置指定target输出post_fix
function(xset_target_debug_postfix TARGET_NAME POSTFIX)
    set_target_properties(${TARGET_NAME} PROPERTIES DEBUG_POSTFIX "${POSTFIX}")
endfunction()

# 设置指定target的include_path
function(xset_target_include_dirs TARGET_NAME)
    set(NEW_INCLUDE_PATHS ${ARGN})
    target_include_directories(${TARGET_NAME} PRIVATE ${NEW_INCLUDE_PATHS})
endfunction()

function(xget_target_include_dirs TARGET_NAME INCLUDE_DIRS)
    get_target_property(_include_dirs ${TARGET_NAME} INCLUDE_DIRECTORIES)
    set(${INCLUDE_DIRS} ${_include_dirs} PARENT_SCOPE)
endfunction()

# 输出指定target的libs
function(xecho_target_link_libs TARGET_NAME)
    get_target_property(dst_link_libs ${TARGET_NAME} LINK_LIBRARIES)
    #string(REPLACE "\n" "," echo_str ${dst_link_libs})
    set(echo_str)
    foreach(lib_name ${dst_link_libs})
        if(echo_str)
            set(echo_str ${echo_str},${lib_name})
        else()
            set(echo_str ${lib_name})
        endif()
    endforeach()
    message("echo target link libs: " "tareget=" ${TARGET_NAME} ",libs=" ${echo_str})
endfunction()

# # 设置编译选项
# if(WIN32)
#     target_compile_options(your_target PRIVATE /your/compile/option/for/windows)
# elseif(APPLE)
#     target_compile_options(your_target PRIVATE -your-compile-option-for-macOS)
# elseif(UNIX)
#     target_compile_options(your_target PRIVATE -your-compile-option-for-linux)
# endif()

# 设置指定target编译选项
# target_compile_options(your_target PRIVATE
#     -Wall
#     -Wextra
#     -Werror
# )


###
# @brief 给目标添加编译选项
function(xset_target_compile_options TARGET_NAME PLATFORM)#OPTIONS
    SET(OPTIONS ${ARGN})
    if(PLATFORM STREQUAL "all" OR PLATFORM STREQUAL "All" OR PLATFORM STREQUAL "common")  # 全平台生效的lib
        target_compile_options(${TARGET_NAME} PRIVATE  ${OPTIONS})
    elseif(PLATFORM STREQUAL "linux" OR PLATFORM STREQUAL "Linux")
        IF(${XCMAKE_TARGET_OS_LINUX})
            target_compile_options(${TARGET_NAME} PRIVATE  ${OPTIONS})
        ENDIF()
    elseif(PLATFORM STREQUAL "windows" OR PLATFORM STREQUAL "Windows")
        IF(${XCMAKE_TARGET_OS_WIN})
            target_compile_options(${TARGET_NAME} PRIVATE  ${OPTIONS})
        ENDIF()
    elseif(PLATFORM STREQUAL "mac" OR PLATFORM STREQUAL "Mac")
        IF(${XCMAKE_TARGET_OS_MAC})
            target_compile_options(${TARGET_NAME} PRIVATE  ${OPTIONS})
        ENDIF()
    endif()
endfunction()